﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmPooupbl : Form
    {
        #region Create Deligate for pass details

        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);

        // add an event of the delegate type
        public event IdentityUpdateHandler IdentityUpdated;

        #endregion

        DataTable GvSelectedDetails = new DataTable();
        DataTable GvUserDetailsGlobel = new DataTable();
        DataTable GvPricelisttbl = new DataTable();

        string Gvbarcode;

        public frmPooupbl(string Pbarcode)
        {
            InitializeComponent();
            Gvbarcode = Pbarcode;
        }

        private void frmPooupbl_Load(object sender, EventArgs e)
        {
            #region GVUserDetailsGlobel--Datatable

            GvUserDetailsGlobel.Columns.Add("Pro_ID");
            GvUserDetailsGlobel.Columns.Add("Pl_Id");
            GvUserDetailsGlobel.Columns.Add("Brand_Name");
            GvUserDetailsGlobel.Columns.Add("Pro_Name");
            GvUserDetailsGlobel.Columns.Add("Pro_Price");
            GvUserDetailsGlobel.Columns.Add("Ret_Price");
            GvUserDetailsGlobel.Columns.Add("PV");
            GvUserDetailsGlobel.Columns.Add("BV");
            GvUserDetailsGlobel.Columns.Add("Quantity");

            #endregion

            #region GvSelectedDetails--Datatable

            GvSelectedDetails.Columns.Add("Pro_ID");
            GvSelectedDetails.Columns.Add("Pl_Id");
            GvSelectedDetails.Columns.Add("Brand_Name");
            GvSelectedDetails.Columns.Add("Pro_Name");
            GvSelectedDetails.Columns.Add("Pro_Price");
            GvSelectedDetails.Columns.Add("Ret_Price");
            GvSelectedDetails.Columns.Add("PV");
            GvSelectedDetails.Columns.Add("BV");
            GvSelectedDetails.Columns.Add("Quantity");

            #endregion

            GridFill();
        }

        #region GridFill
        public void GridFill()
        {

            //if (Gvproductid.Length == 7)
            //{
            GvPricelisttbl = ProductDAO.GetProductByIdForPos(Gvbarcode, "0");
                for (int i = 0; i <= GvPricelisttbl.Rows.Count - 1; i++)
                {
                    GvUserDetailsGlobel.Rows.Add(GvUserDetailsGlobel.NewRow());
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][0] = GvPricelisttbl.Rows[i][0].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][1] = GvPricelisttbl.Rows[i][1].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][2] = GvPricelisttbl.Rows[i][2].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][3] = GvPricelisttbl.Rows[i][3].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][4] = GvPricelisttbl.Rows[i][4].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][5] = GvPricelisttbl.Rows[i][5].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][6] = GvPricelisttbl.Rows[i][6].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][7] = GvPricelisttbl.Rows[i][7].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][8] = GvPricelisttbl.Rows[i][8].ToString();

                }

                dgvpoppricelist.DataSource = GvUserDetailsGlobel;

            //}
            //else
            //{

            //}

        }
        #endregion

        private void dgvpoppricelist_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar ==(char)Keys.Space)
            {

                if (MessageBox.Show("Will you really Add the item?", "Warning", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    if (!dgvpoppricelist.Rows[dgvpoppricelist.CurrentRow.Index].Cells[0].Value.ToString().Equals(""))
                    {
                        string LvProductid = dgvpoppricelist.Rows[dgvpoppricelist.CurrentRow.Index].Cells[0].Value.ToString();
                        string LvPl_Id = dgvpoppricelist.Rows[dgvpoppricelist.CurrentRow.Index].Cells[1].Value.ToString();

                        GvSelectedDetails = ProductDAO.GetProductByIdandplidForPos(LvProductid, LvPl_Id);//pass datatable what we select using prodduct id and Pl id

                        #region selected datatable
                        IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(GvSelectedDetails);

                        // raise the event with the updated arguments
                        IdentityUpdated(this, args);

                        this.Dispose();
                        #endregion
                    }   
                   
                }
                else { MessageBox.Show("Nooooooo :)" + e.KeyChar + "" + dgvpoppricelist.Rows[dgvpoppricelist.CurrentRow.Index].Cells[0].Value.ToString()); }
            }
            else
            {
                
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

    }
    public class IdentityUpdateEventArgs : System.EventArgs
    {
        // add local member variable to hold text
        private DataTable passtable;



        // class constructor
        public IdentityUpdateEventArgs(DataTable pdata)
        {
            this.passtable = pdata;
        }

        // Properties - Accessible by the listener

        public DataTable Passtable
        {
            get { return passtable; }
            set { passtable = value; }
        }
    }

}
